package com.gxa.community.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.community.admin.mapper.OperateTicketGoodsRelMapper;
import com.gxa.community.admin.mapper.OperateTicketMapper;
import com.gxa.community.admin.service.OperateTicketGoodsRelService;
import com.gxa.community.admin.service.OperateTicketService;
import com.gxa.community.core.pojo.domain.*;
import com.gxa.community.core.pojo.vo.TicketsAddVo;
import com.gxa.community.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 优惠券信息表 服务实现类
 * </p>
 *
 * @author XYJ
 * @since 2022-07-20
 */
@Service
public class OperateTicketServiceImpl extends ServiceImpl<OperateTicketMapper, OperateTicket> implements OperateTicketService {
    @Autowired
    private OperateTicketMapper operateTicketMapper;
    @Autowired
    private OperateTicketGoodsRelService operateTicketGoodsRelService;
    @Autowired
    private OperateTicketGoodsRelMapper operateTicketGoodsRelMapper;
    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate ;
    public static final String GOODSDEL = "GoodsDel:";

    @Override
    public R selectAll(int page, int size) {
        Page<OperateTicket> page1 = new Page<>(page, size);
        Page<OperateTicket> operateFull = this.page(page1, null);
        List<OperateTicket> list = operateFull.getRecords();
        List<OperateTicket> operateTickets = new ArrayList<>();
        for (OperateTicket operateTicket : list) {
            OperateTicket operateTicket1 = new OperateTicket();
//            UpdateWrapper<OperateTicket> wrapper = new UpdateWrapper<>();
//            wrapper.eq("ticket_no",operateTicket.getTicketNo());
            operateTicket1.setId(operateTicket.getId());
            operateTicket1.setTicketNo(operateTicket.getTicketNo());
            operateTicket1.setName(operateTicket.getName());
            operateTicket1.setType(operateTicket.getType());
            operateTicket1.setUseType(operateTicket.getUseType());
            operateTicket1.setNum(operateTicket.getNum());
            operateTicket1.setPrice(operateTicket.getPrice());
            operateTicket1.setStint(operateTicket.getStint());
            operateTicket1.setUseStint(operateTicket.getUseStint());
            operateTicket1.setFullPrice(operateTicket.getFullPrice());

            Long startTime = operateTicket.getStartTime();//开始时间
            operateTicket1.setStartTime(startTime);
            Long endTime = operateTicket.getEndTime();//结束时间
            operateTicket1.setEndTime(endTime);
            operateTicket1.setRemark(operateTicket.getRemark());
            operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
            //更新状态
            long nowTime = new Date().getTime();//获取当前时间
            if (startTime<=nowTime && nowTime <= endTime){
                operateTicket1.setStatus(0);//未过期
                operateTicketMapper.updatestatus(0,operateTicket.getId());
            }if (nowTime<startTime){
               operateTicket1.setStatus(0);//未过期
                operateTicketMapper.updatestatus(0,operateTicket.getId());
            }if (nowTime>endTime){
                operateTicket1.setStatus(1);//已过期
                operateTicketMapper.updatestatus(1,operateTicket.getId());
            }
            operateTickets.add(operateTicket1);
        }

        return R.ok().put("data", operateTickets);
    }

    @Override
    public R addTicket(TicketsAddVo ticketsAddVo) {
        if(ticketsAddVo!=null){
            //将ticketsAddVo里的数据全部取出set进 OperateTicket中
            OperateTicket operateTicket1 = new OperateTicket();
            operateTicket1.setTicketNo(ticketsAddVo.getTicketNo());
            operateTicket1.setName(ticketsAddVo.getName());
            operateTicket1.setType(ticketsAddVo.getType());
            operateTicket1.setUseType(ticketsAddVo.getUseType());
            operateTicket1.setNum(ticketsAddVo.getNum());
            operateTicket1.setPrice(ticketsAddVo.getPrice());
            operateTicket1.setStint(ticketsAddVo.getStint());
            operateTicket1.setUseStint(ticketsAddVo.getUseStint());
            operateTicket1.setFullPrice(ticketsAddVo.getFullPrice());
            operateTicket1.setStartTime(ticketsAddVo.getStartTime());
            operateTicket1.setEndTime(ticketsAddVo.getEndTime());
            operateTicket1.setRemark(ticketsAddVo.getRemark());
            if(ticketsAddVo.getStint()==2){
                operateTicket1.setUseThreshold("无限制");
            }else {
                operateTicket1.setUseThreshold("满"+ticketsAddVo.getPrice()+"减"+ticketsAddVo.getFullPrice());
            }
            //添加成功前 通知前台删除redis
//            String message = "活动改变请删除redis";
            jmsMessagingTemplate.convertAndSend(GOODSDEL,new ArrayList<>());
            boolean save = this.save(operateTicket1);
            //添加相关商品
            //如果type==1则不需要存储商品
            if(ticketsAddVo.getType()==1)
            {
                return R.ok("添加满减成功，全品类商品都可用");}
            //获得优惠券商品的list
            List<OperateTicketGoodsRel> operateTicketGoodsRel = ticketsAddVo.getOperateTicketGoodsRel();
            boolean b = operateTicketGoodsRelService.saveBatch(operateTicketGoodsRel);
            if (save&&b){
                return R.ok("添加满减成功");
            }return R.error("添加失败，请检查数据");
        }
        return R.error(502,"添加失败，传入的实体为null");
    }

    @Override
    public R delTicket(String ticketNo) {
        //先根据ticketNo找到status
        QueryWrapper<OperateTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("ticket_no",ticketNo);
        OperateTicket operateTicket = operateTicketMapper.selectOne(wrapper);
        if (operateTicket!=null){
            Integer status = operateTicket.getStatus();
            if (status==1){
                //通过fullNo进行删除
                HashMap<String,Object> map = new HashMap<>();
                map.put("ticket_no",ticketNo);
                //删除前前台redis删除
//            String message = "活动改变请删除redis";
                jmsMessagingTemplate.convertAndSend(GOODSDEL,new ArrayList<>());
                int i = operateTicketMapper.deleteByMap(map);
                //删除相关商品
                operateTicketGoodsRelMapper.deleteByMap(map);
                if (i>0){
                    return R.ok("删除成功");
                }return R.error(407,"删除失败，请检查编号");
            }return R.error(306,"该优惠券未过期，不能删除");
            }
        return R.error(503,"失败,请检查满减编号是否有误");
    }

    @Override
    public R preEdit(String ticketNo) {
        //根据ticketNo查询1条实体类
        QueryWrapper<OperateTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("ticket_no",ticketNo);
        OperateTicket operateTicket = operateTicketMapper.selectOne(wrapper);
        if (operateTicket!=null){

            QueryWrapper<OperateTicketGoodsRel> goodawrapper = new QueryWrapper<>();
            goodawrapper.eq("ticket_no",ticketNo);
            //获取相关商品集合
            List<OperateTicketGoodsRel> operateTicketGoodsRels = operateTicketGoodsRelMapper.selectList(goodawrapper);
            //将基础信息返回
            TicketsAddVo ticketsAddVo = new TicketsAddVo();
            ticketsAddVo.setTicketNo(operateTicket.getTicketNo());
            ticketsAddVo.setName(operateTicket.getName());
            ticketsAddVo.setType(operateTicket.getType());
            ticketsAddVo.setUseType(operateTicket.getUseType());
            ticketsAddVo.setNum(operateTicket.getNum());
            ticketsAddVo.setPrice(operateTicket.getPrice());
            ticketsAddVo.setStint(operateTicket.getStint());
            ticketsAddVo.setUseStint(operateTicket.getUseStint());
            ticketsAddVo.setFullPrice(operateTicket.getFullPrice());
            ticketsAddVo.setStartTime(operateTicket.getStartTime());
            ticketsAddVo.setEndTime(operateTicket.getEndTime());
            ticketsAddVo.setRemark(operateTicket.getRemark());
            //回显相关商品
            ticketsAddVo.setOperateTicketGoodsRel(operateTicketGoodsRels);
            return R.ok().put("data",ticketsAddVo);
        }

        return R.error(602,"无优惠券信息请检查优惠券编号是否有误");
    }

    @Override
    public R editTicket(TicketsAddVo ticketsAddVo) {
        if(ticketsAddVo!=null){
            String ticketNo = ticketsAddVo.getTicketNo();
//            根据ticketNo查status
            QueryWrapper<OperateTicket> wrapper = new QueryWrapper<>();
            wrapper.eq("ticket_no",ticketNo);
            OperateTicket operateTicket = operateTicketMapper.selectOne(wrapper);
            if (operateTicket!=null) {
                Integer status = operateTicket.getStatus();
                if (status==1){
                    HashMap<String,Object> map =new HashMap<>();
                    map.put("ticket_no",ticketNo);
                    int i = operateTicketMapper.deleteByMap(map);
                    System.err.println("删除"+i);
                    //删除相关商品
                    int i1 = operateTicketGoodsRelMapper.deleteByMap(map);
                    System.err.println("删除"+i1);
                    //开始进行添加
                    //将ticketsAddVo里的数据全部取出set进 OperateTicket中
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setTicketNo(ticketsAddVo.getTicketNo());
                    operateTicket1.setName(ticketsAddVo.getName());
                    operateTicket1.setType(ticketsAddVo.getType());
                    operateTicket1.setUseType(ticketsAddVo.getUseType());
                    operateTicket1.setNum(ticketsAddVo.getNum());
                    operateTicket1.setPrice(ticketsAddVo.getPrice());
                    operateTicket1.setStint(ticketsAddVo.getStint());
                    operateTicket1.setUseStint(ticketsAddVo.getUseStint());
                    operateTicket1.setFullPrice(ticketsAddVo.getFullPrice());
                    operateTicket1.setStartTime(ticketsAddVo.getStartTime());
                    operateTicket1.setEndTime(ticketsAddVo.getEndTime());
                    operateTicket1.setRemark(ticketsAddVo.getRemark());
                    if(ticketsAddVo.getStint()==2){
                        operateTicket1.setUseThreshold("无限制");
                    }else {
                        operateTicket1.setUseThreshold("满"+ticketsAddVo.getPrice()+"减"+ticketsAddVo.getFullPrice());
                    }
                    //删除前前台redis删除
//            String message = "活动改变请删除redis";
                    jmsMessagingTemplate.convertAndSend(GOODSDEL,new ArrayList<>());
                    boolean save = this.save(operateTicket1);
                    //添加相关商品
                    //获得优惠券商品的list
                    List<OperateTicketGoodsRel> operateTicketGoodsRel = ticketsAddVo.getOperateTicketGoodsRel();
                    boolean b = operateTicketGoodsRelService.saveBatch(operateTicketGoodsRel);
                    if (save&&b){
                        return R.ok("修改优惠券成功");
                    }return R.error("修改失败，请检查数据");
                }return R.error("该优惠券未过期，不能修改");
            }

        }
        return R.error(502,"修改失败，传入的实体为null");
    }

    @Override
    public R textTicket(String name, Integer status, Long startTime, Long endTime, Integer page, Integer size) {
        System.err.println("1"+name+"==="+status +"==="+startTime+"==="+ endTime+"==="+ page  +"==="+size);
        if (page!=null&&size!=null) {
            Page<OperateTicket> page1 = new Page<>(page, size);
            QueryWrapper<OperateTicket> wrapper = new QueryWrapper<>();
            if (status == null) {
                return R.error(500, "status不能为null 0-未过期,1-已过期,2-全部");
            } else
            if (status==2&&name==null&&startTime==null&&endTime==null){
                Page<OperateTicket> operateFull = this.page(page1, null);
                List<OperateTicket> list = operateFull.getRecords();
                List<OperateTicket> operateTickets = new ArrayList<>();
                for (OperateTicket operateTicket : list) {
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setId(operateTicket.getId());
                    operateTicket1.setTicketNo(operateTicket.getTicketNo());
                    operateTicket1.setName(operateTicket.getName());
                    operateTicket1.setType(operateTicket.getType());
                    operateTicket1.setUseType(operateTicket.getUseType());
                    operateTicket1.setNum(operateTicket.getNum());
                    operateTicket1.setPrice(operateTicket.getPrice());
                    operateTicket1.setStint(operateTicket.getStint());
                    operateTicket1.setUseStint(operateTicket.getUseStint());
                    operateTicket1.setFullPrice(operateTicket.getFullPrice());
                    Long startTime1 = operateTicket.getStartTime();//开始时间
                    operateTicket1.setStartTime(startTime1);
                    Long endTime1 = operateTicket.getEndTime();//结束时间
                    operateTicket1.setEndTime(endTime1);
                    operateTicket1.setRemark(operateTicket.getRemark());
                    operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
                    //更新状态
                    long nowTime = new Date().getTime();//获取当前时间
                    if (startTime1<=nowTime && nowTime <= endTime1){
                        operateTicket1.setStatus(0);//未过期
                        operateTicketMapper.updatestatus(0,operateTicket.getId());
                    }if (nowTime<startTime1){
                        operateTicket1.setStatus(0);//未过期
                        operateTicketMapper.updatestatus(0,operateTicket.getId());
                    }if (nowTime>endTime1){
                        operateTicket1.setStatus(1);//已过期
                        operateTicketMapper.updatestatus(1,operateTicket.getId());
                    }
                    operateTickets.add(operateTicket1);
                }
                return R.ok().put("data", operateTickets);
            }else
            if (name.length()!=0&&startTime==null&&endTime==null&&status==2){
                //根据fullNo查询1条实体类数据
                wrapper.like("name",name);
                Page<OperateTicket> operateFull = this.page(page1, wrapper);
                List<OperateTicket> list = operateFull.getRecords();
                List<OperateTicket> operateTickets = new ArrayList<>();
                for (OperateTicket operateTicket : list) {
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setId(operateTicket.getId());
                    operateTicket1.setTicketNo(operateTicket.getTicketNo());
                    operateTicket1.setName(operateTicket.getName());
                    operateTicket1.setType(operateTicket.getType());
                    operateTicket1.setUseType(operateTicket.getUseType());
                    operateTicket1.setNum(operateTicket.getNum());
                    operateTicket1.setPrice(operateTicket.getPrice());
                    operateTicket1.setStint(operateTicket.getStint());
                    operateTicket1.setUseStint(operateTicket.getUseStint());
                    operateTicket1.setFullPrice(operateTicket.getFullPrice());
                    Long startTime1 = operateTicket.getStartTime();//开始时间
                    operateTicket1.setStartTime(startTime1);
                    Long endTime1 = operateTicket.getEndTime();//结束时间
                    operateTicket1.setEndTime(endTime1);
                    operateTicket1.setRemark(operateTicket.getRemark());
                    operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
                    operateTicket1.setStatus(status);
                    operateTickets.add(operateTicket1);
                }
                return R.ok().put("data", operateTickets);
            }else
            if (name.length()!=0&&startTime!=null&&endTime!=null&&status==2){
                //根据fullNo查询1条实体类数据
                wrapper.like("name",name);
                wrapper.ge("start_time",startTime);
                wrapper.le("end_time",endTime);
                Page<OperateTicket> operateFull = this.page(page1, wrapper);
                List<OperateTicket> list = operateFull.getRecords();
                List<OperateTicket> operateTickets = new ArrayList<>();
                for (OperateTicket operateTicket : list) {
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setId(operateTicket.getId());
                    operateTicket1.setTicketNo(operateTicket.getTicketNo());
                    operateTicket1.setName(operateTicket.getName());
                    operateTicket1.setType(operateTicket.getType());
                    operateTicket1.setUseType(operateTicket.getUseType());
                    operateTicket1.setNum(operateTicket.getNum());
                    operateTicket1.setPrice(operateTicket.getPrice());
                    operateTicket1.setStint(operateTicket.getStint());
                    operateTicket1.setUseStint(operateTicket.getUseStint());
                    operateTicket1.setFullPrice(operateTicket.getFullPrice());
                    Long startTime1 = operateTicket.getStartTime();//开始时间
                    operateTicket1.setStartTime(startTime1);
                    Long endTime1 = operateTicket.getEndTime();//结束时间
                    operateTicket1.setEndTime(endTime1);
                    operateTicket1.setRemark(operateTicket.getRemark());
                    operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
                    operateTicket1.setStatus(status);
                    operateTickets.add(operateTicket1);
                }
                return R.ok().put("data", operateTickets);
            }else
            if (name.length()==0&&startTime!=null&&endTime!=null&&status==2){
                //根据fullNo查询1条实体类数据
                wrapper.ge("start_time",startTime);
                wrapper.le("end_time",endTime);
                Page<OperateTicket> operateFull = this.page(page1, wrapper);
                List<OperateTicket> list = operateFull.getRecords();
                List<OperateTicket> operateTickets = new ArrayList<>();
                for (OperateTicket operateTicket : list) {
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setId(operateTicket.getId());
                    operateTicket1.setTicketNo(operateTicket.getTicketNo());
                    operateTicket1.setName(operateTicket.getName());
                    operateTicket1.setType(operateTicket.getType());
                    operateTicket1.setUseType(operateTicket.getUseType());
                    operateTicket1.setNum(operateTicket.getNum());
                    operateTicket1.setPrice(operateTicket.getPrice());
                    operateTicket1.setStint(operateTicket.getStint());
                    operateTicket1.setUseStint(operateTicket.getUseStint());
                    operateTicket1.setFullPrice(operateTicket.getFullPrice());
                    Long startTime1 = operateTicket.getStartTime();//开始时间
                    operateTicket1.setStartTime(startTime1);
                    Long endTime1 = operateTicket.getEndTime();//结束时间
                    operateTicket1.setEndTime(endTime1);
                    operateTicket1.setRemark(operateTicket.getRemark());
                    operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
                    operateTicket1.setStatus(status);
                    operateTickets.add(operateTicket1);
                }
                return R.ok().put("data", operateTickets);
            }else
            if(name.length()!=0&&startTime!=null&&endTime!=null&&status!=2){
                //根据fullNo查询1条实体类数据
                wrapper.like("name",name);
                wrapper.ge("start_time",startTime);
                wrapper.le("end_time",endTime);
                wrapper.eq("status",status);
                Page<OperateTicket> operateFull = this.page(page1, wrapper);
                List<OperateTicket> list = operateFull.getRecords();
                List<OperateTicket> operateTickets = new ArrayList<>();
                for (OperateTicket operateTicket : list) {
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setId(operateTicket.getId());
                    operateTicket1.setTicketNo(operateTicket.getTicketNo());
                    operateTicket1.setName(operateTicket.getName());
                    operateTicket1.setType(operateTicket.getType());
                    operateTicket1.setUseType(operateTicket.getUseType());
                    operateTicket1.setNum(operateTicket.getNum());
                    operateTicket1.setPrice(operateTicket.getPrice());
                    operateTicket1.setStint(operateTicket.getStint());
                    operateTicket1.setUseStint(operateTicket.getUseStint());
                    operateTicket1.setFullPrice(operateTicket.getFullPrice());
                    Long startTime1 = operateTicket.getStartTime();//开始时间
                    operateTicket1.setStartTime(startTime1);
                    Long endTime1 = operateTicket.getEndTime();//结束时间
                    operateTicket1.setEndTime(endTime1);
                    operateTicket1.setRemark(operateTicket.getRemark());
                    operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
                    operateTicket1.setStatus(status);
                    operateTickets.add(operateTicket1);
                }
                return R.ok().put("data", operateTickets);
            }else
            if(name.length()==0&&startTime==null&&endTime==null&&status!=2){
                //根据fullNo查询1条实体类数据
                wrapper.eq("status",status);
                Page<OperateTicket> operateFull = this.page(page1, wrapper);
                List<OperateTicket> list = operateFull.getRecords();
                List<OperateTicket> operateTickets = new ArrayList<>();
                for (OperateTicket operateTicket : list) {
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setId(operateTicket.getId());
                    operateTicket1.setTicketNo(operateTicket.getTicketNo());
                    operateTicket1.setName(operateTicket.getName());
                    operateTicket1.setType(operateTicket.getType());
                    operateTicket1.setUseType(operateTicket.getUseType());
                    operateTicket1.setNum(operateTicket.getNum());
                    operateTicket1.setPrice(operateTicket.getPrice());
                    operateTicket1.setStint(operateTicket.getStint());
                    operateTicket1.setUseStint(operateTicket.getUseStint());
                    operateTicket1.setFullPrice(operateTicket.getFullPrice());
                    Long startTime1 = operateTicket.getStartTime();//开始时间
                    operateTicket1.setStartTime(startTime1);
                    Long endTime1 = operateTicket.getEndTime();//结束时间
                    operateTicket1.setEndTime(endTime1);
                    operateTicket1.setRemark(operateTicket.getRemark());
                    operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
                    operateTicket1.setStatus(status);
                    operateTickets.add(operateTicket1);
                }
                return R.ok().put("data", operateTickets);
            } else
            if(name.length()==0&&startTime!=null&&endTime!=null&&status!=2){
                //根据fullNo查询1条实体类数据
                wrapper.eq("status",status);
                wrapper.ge("start_time",startTime);
                wrapper.le("end_time",endTime);
                Page<OperateTicket> operateFull = this.page(page1, wrapper);
                List<OperateTicket> list = operateFull.getRecords();
                List<OperateTicket> operateTickets = new ArrayList<>();
                for (OperateTicket operateTicket : list) {
                    OperateTicket operateTicket1 = new OperateTicket();
                    operateTicket1.setId(operateTicket.getId());
                    operateTicket1.setTicketNo(operateTicket.getTicketNo());
                    operateTicket1.setName(operateTicket.getName());
                    operateTicket1.setType(operateTicket.getType());
                    operateTicket1.setUseType(operateTicket.getUseType());
                    operateTicket1.setNum(operateTicket.getNum());
                    operateTicket1.setPrice(operateTicket.getPrice());
                    operateTicket1.setStint(operateTicket.getStint());
                    operateTicket1.setUseStint(operateTicket.getUseStint());
                    operateTicket1.setFullPrice(operateTicket.getFullPrice());
                    Long startTime1 = operateTicket.getStartTime();//开始时间
                    operateTicket1.setStartTime(startTime1);
                    Long endTime1 = operateTicket.getEndTime();//结束时间
                    operateTicket1.setEndTime(endTime1);
                    operateTicket1.setRemark(operateTicket.getRemark());
                    operateTicket1.setUseThreshold(operateTicket.getUseThreshold());
                    operateTicket1.setStatus(status);
                    operateTickets.add(operateTicket1);
                }
                return R.ok().put("data", operateTickets);
            }
            else return R.error(505,"startTime和endTime不能单独一个为null");
        }
        return R.error(601,"page和size不能为null");
    }
}
